package com.uplift.mgt.building.controller;

import com.uplift.common.base.ServiceReslut;
import com.uplift.common.result.IResultable;
import com.uplift.common.result.ResultWrapper4Layui;
import com.uplift.mgt.building.model.BuildingModel;
import com.uplift.mgt.building.model.RealtyLease;
import com.uplift.mgt.building.service.IBuildingService;
import com.uplift.mgt.building.service.IRealtyLeaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 租约信息相关
 * 
 * @author bcwang
 *
 */
@Controller
@RequestMapping(value = "/realty-lease")
public class RealtyLeaseController {

	@Autowired
	private IRealtyLeaseService realtyLeaseService;

	/**
	 * 查询租约信息
	 *
	 * @param request
	 * @param realtyLease
	 * @return IResultable
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public IResultable list(HttpServletRequest request, RealtyLease realtyLease) {
		try {
			List<Map<String, Object>> list = this.realtyLeaseService.queryRealtyLease(realtyLease);
			return ResultWrapper4Layui.wrapListPage(list, ResultWrapper4Layui.SUCC, "查询成功");
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapListPage(null, ResultWrapper4Layui.FAIL, "查询失败");
		}
	}

	/**
	 * 新增租约记录
	 *
	 * @param request
	 * @param realtyLease
	 * @return IResultable
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public IResultable save(HttpServletRequest request, RealtyLease realtyLease) {
		try {
			ServiceReslut serviceReslut = this.realtyLeaseService.addRealtyLease(realtyLease);
			if (serviceReslut.getCode() > 0) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "保存成功");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "保存失败," + serviceReslut.getMessage());
			}
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "保存失败");
		}
	}

	/**
	 * 删除租约
	 *
	 * @param request
	 * @param realtyLease
	 * @return IResultable
	 */
	@RequestMapping(value = "/remove")
	@ResponseBody
	public IResultable remove(HttpServletRequest request, RealtyLease realtyLease) {
		try {
			ServiceReslut serviceReslut = this.realtyLeaseService.deleteRealtyLease(realtyLease);
			if (serviceReslut.getCode() > 0) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "删除成功");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "删除失败," + serviceReslut.getMessage());
			}
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "删除失败");
		}
	}

	/**
	 * 更新租约信息
	 *
	 * @param request
	 * @param realtyLease
	 * @return IResultable
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public IResultable update(HttpServletRequest request, RealtyLease realtyLease) {
		try {
			ServiceReslut serviceReslut = this.realtyLeaseService.updateRealtyLease(realtyLease);
			if (serviceReslut.getCode() > 0) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "更新成功");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "更新失败," + serviceReslut.getMessage());
			}
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "更新失败");
		}
	}

	/**
	 * 结束租约信息
	 *
	 * @param request
	 * @param realtyLease
	 * @return IResultable
	 */
	@RequestMapping(value = "/end-lease")
	@ResponseBody
	public IResultable endLease(HttpServletRequest request, RealtyLease realtyLease) {
		try {
			ServiceReslut serviceReslut = this.realtyLeaseService.endRealtyLease(realtyLease);
			if (serviceReslut.getCode() > 0) {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "结束成功");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "结束失败," + serviceReslut.getMessage());
			}
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "结束失败");
		}
	}

	/**
	 * 获取单个租约信息
	 *
	 * @param request
	 * @param realtyLease
	 * @return IResultable
	 */
	@RequestMapping(value = "/get-one")
	@ResponseBody
	public IResultable getOne(HttpServletRequest request, RealtyLease realtyLease) {
		try {
			List<Map<String, Object>> list = this.realtyLeaseService.queryRealtyLease(realtyLease);
			if (list!=null && list.size()>0) {

				Map<String, Object> data = list.get(0);

				data.put("ads", realtyLeaseService.queryRealtyLeaseAd(data.get("id").toString()));

				return ResultWrapper4Layui.wrapMap(data, ResultWrapper4Layui.SUCC, "查询成功");
			} else {
				return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "查询成功," );
			}
		} catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, "查询失败");
		}
	}

	/**
	 * 导入租约
	 * @return
	 */
	@RequestMapping(value = "/import-lease")
	@ResponseBody
	public IResultable importLease(MultipartHttpServletRequest request) {
		try {
			this.realtyLeaseService.importLease(request);
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.SUCC, "导入成功");
		}catch (Exception e) {
			return ResultWrapper4Layui.wrapMap(null, ResultWrapper4Layui.FAIL, e.getMessage());
		}
	}
}
